package com.hod.client.bedtype;

import com.google.gwt.core.client.GWT;
import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.grid.AbstractSelectionModel;
import com.gwtext.client.widgets.grid.RowSelectionModel;
import com.gwtext.client.widgets.grid.event.RowSelectionListenerAdapter;
import com.hod.client.HOD;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.frontend.CRUD;
import com.hod.client.frontend.HODForm;
import com.hod.client.lodging.Lodging;
import com.hod.client.room.RoomCRUD;

public class BedTypeCRUD extends CRUD {
  
  
  	//protected final TextField txtIdBedType = new TextField("idBedType", "idBedType", 150);
  	public static void createListBox(Integer idSelected,ListBox lb){
		GearsDataStore ds = (GearsDataStore) GWT.create(BedType.class);
		try {
			//Obtengo todos los hospedajes validos
			GearsQuery q = ds.getNamedQuery(BedType.FIND_ALL);
			BedType[] data = (BedType[]) q.execute();
			lb.clear();
			if (data.length>0){
				for (BedType bt:data){
					lb.addItem(bt.getDescription(),bt.getIdBedType().toString());
					if (idSelected.equals(bt.getIdBedType())){
						lb.setSelectedIndex(lb.getItemCount()-1);
					}
				}
			}else{
				lb.addItem("No hay tipos de cama disponibles");
			}
		} catch (DatabaseException e) {			
			// TODO Auto-generated catch block
		}
		
	}
  	
      
  	public BedTypeCRUD() {
  		ds = (GearsDataStore) GWT.create(BedType.class);
  		gridPanel = new BedTypeGrid(false,ds);		
  	}
  		
  	@Override
  	protected void buildPanelSearch() {
  
  		
  	}
  
  	@Override
  	protected AbstractSelectionModel createSelectionModel() {
  		// TODO Auto-generated method stub
  		final RowSelectionModel sm = new RowSelectionModel(true);
          sm.addListener(new RowSelectionListenerAdapter() {
          	public void onRowSelect(RowSelectionModel sm, int rowIndex, Record record) {
                  formPanel.updateDataControls(record);
              }
          });
          return sm;
  	}
  
  
  	@Override
  	protected Widget formTabs() {
  		// TODO Auto-generated method stub
  		return null;
  	}


	@Override
	public HODForm createFormPanel() {
		// TODO Auto-generated method stub
		if (formPanel==null) formPanel = new BedTypeFormPanel(ds);
		return formPanel;
	}
	
  
}
